package Game
{
	import Utils.CollisionDetection;
	import Utils.Compute;
	
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import spark.components.BorderContainer;
	import spark.primitives.BitmapImage;

	public class Player
	{
		private var _tank:Tank;
		
		private var _isPlayingForward:Boolean = false;
		private var _isPlayingBackward:Boolean = false;
		private var _isPlayingRight:Boolean = false;
		private var _isPlayingLeft:Boolean = false;
		
		private var _isStopped:Boolean = false;
		
		private var _cursorX:Number;
		private var _cursorY:Number;
		
		private var _gridBitmapImage:BitmapImage = new BitmapImage();
		private var _mapWorld:BorderContainer = new BorderContainer();;
		
		public function Player()
		{
			_tank = new Tank();
		}
		
		public function get tank():Tank
		{
			return _tank;
		}
		
		public function get grid():BitmapImage
		{
			return _gridBitmapImage;
		}
		
		public function set map(mapWorld:BorderContainer):void
		{
			_mapWorld = mapWorld;
		}
		
		public function get map():BorderContainer
		{
			return _mapWorld;
		}

		public function playForward():void
		{
			_isPlayingForward = true;
		}
		
		public function stopForward():void
		{
			_isPlayingForward = false;
		}
		
		public function playBackward():void
		{
			_isPlayingBackward= true;
		}
		
		public function stopBackward():void
		{
			_isPlayingBackward = false;
		}
		
		public function playLeft():void
		{
			_isPlayingLeft = true;
		}
		
		public function stopLeft():void
		{
			_isPlayingLeft = false;
		}
		
		public function playRight():void
		{
			_isPlayingRight= true;
		}
		
		public function stopRight():void
		{
			_isPlayingRight = false;
		}
		
		public function lookAt(cursorX:Number, cursorY:Number):void
		{
			_cursorX = cursorX;
			_cursorY = cursorY;
		}
		
		/**
		 * Call after each ENTER_FRAME
		 * */
		public function step():void
		{
			var oldRotationZ:Number = this._tank.rotationZ;
			var oldX:Number = this._tank.x
			var oldY:Number = this._tank.y
			
			var dy:int = 0;
			var dz:int = 0;
			if (_isPlayingForward==true)
			{
				dy -= 4;
			}
			
			if (_isPlayingBackward==true)
			{
				dy += 3;
			}
			
			if (_isPlayingLeft==true)
			{
				dz -= 2;
			}
			
			if (_isPlayingRight==true)
			{
				dz += 2;
			}
			
			// position tank changed
			if(dy!=0 || dz!=00)
			{
				var radians:Number = (this._tank.rotationZ - 90) * Math.PI / 180;
				
				var offsetX:Number = Math.cos(radians) * dy;
				var offsetY:Number = Math.sin(radians) * dy;
				
				this._tank.rotationZ += dz;
				
				this._tank.x -= offsetX;
				this._tank.y -= offsetY;
				
				var xGrid:int = this._tank.x/30;
				var yGrid:int = this._tank.y/30;
				
				var xGridMin:int = Math.max(0, xGrid-2);
				var yGridMin:int = Math.max(0, yGrid-2);
				
				var xGridMax:int = Math.min(32, xGrid+2);
				var yGridMax:int = Math.min(20, yGrid+2);

				var suceptibleCollision:Boolean = false;

				for (var i:int = xGridMin; i <= xGridMax; i++)
				{
					for (var j:int = yGridMin; j <= yGridMax; j++)
					{
						if(_gridBitmapImage.source.bitmapData.getPixel(i,j) == 0xFF0000)
						{
							if(Compute.distance(new Point((i*30)+15, (j*30)+15), new Point(this._tank.x, this._tank.y))<64) //env. (15+30)*sqrt(2)
							{ 
								suceptibleCollision = true
								break;
							}
						}
					}
					if(suceptibleCollision)
					{
						break;
					}
				} 
				
				if (suceptibleCollision){
					
					var clipRect:Rectangle = new Rectangle();
					clipRect.x = xGridMin*30;
					clipRect.y = yGridMin*30;
					
					clipRect.width = (xGridMax-xGridMin+1)*30;
					clipRect.height = (yGridMax-yGridMin+1)*30;
					
					if (CollisionDetection.checkForCollision(_mapWorld, this._tank.base, true, clipRect, 0)!=null)
					{
						this._tank.x = oldX;
						this._tank.y = oldY;
						this._tank.rotationZ = oldRotationZ;
						
						if(!_isStopped)
						{
							this._tank.x += offsetX/2;
							this._tank.y += offsetY/2;
						}
						_isStopped=true;
						
					}else
					{
						_isStopped=false;
					}
				}
			}
			
			this._tank.lookAt(this._cursorX, this._cursorY);
		}
		
		
	}
}